Modify Bold Code

from rest_framework import serializers

from todo.models import Todo

class TodoSerializer(serializers.ModelSerializer):

...

class TodoToggleCompleteSerializer(serializers.ModelSerializer):

class Meta:

model = Todo

fields = ['id'] # why need to show id?

read_only_fields = ['title','memo','created','completed']

Because the TodoToggleCompleteSerializer doesn’t receive and update any of the fields values from the endpoint

(it just toggles completed), we set the fields to read only by specifying them in the Meta shortcut option

read_only_fields. read_only_fields allows us to specify multiple fields as read-only.

Testing Your App

To test the toggle complete endpoint in the browser, go to localhost:8000/api/todos/<todo_id>/complete e.g.:

localhost:8000/api/todos/2/complete and click on ‘PUT’ (fig. 1).

Figure 1

And if you list the todos again, that todo will be marked as complete i.e. completed: true (fig. 2)